package org.nicocube.airain.domain.server.utils;

import org.nicocube.airain.domain.client.data.Storable;
import org.nicocube.airain.domain.client.data.StorageException;
import org.nicocube.airain.domain.client.utils.SequenceHolder;
import org.nicocube.airain.domain.server.data.Storage;

/**
 * This is implementation of {@link SequenceHolder} based on 
 * 
 * @author nicolas
 * @version 0.1
 */
public class SequenceHolderImpl implements SequenceHolder,Storable {
	
	@SuppressWarnings("unused") 
	private String className;// this field is just used to differenciate sequence in the Container
	private long val;
	transient private Storage<SequenceHolderImpl> db;
	
	public SequenceHolderImpl(String className,Storage<SequenceHolderImpl> storage) throws StorageException {
		this.className = className;
		val = 0;
		this.db = storage;
	}
	public long currVal() {
		return val;
	}
	public long nextVal() throws StorageException {
		++val;
		db.save(this);
		return val; 
	}
	public SequenceHolderImpl setStorage(Storage<SequenceHolderImpl> db) {
		this.db = db;
		return this;
	}
}
